<!DOCTYPE html>
<html>
<head>
    <title>怀旧太空射击游戏</title>
    <style>
        canvas {
            border: 2px solid white;
            background: black;
        }
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background: #333;
            color: white;
        }
    </style>
</head>
<body>
    <canvas id="gameCanvas" width="800" height="600"></canvas>

    <script>
        const canvas = document.getElementById('gameCanvas');
        const ctx = canvas.getContext('2d');
        
        // 玩家属性
        const player = {
            x: canvas.width/2,
            y: canvas.height - 50,
            width: 40,
            height: 40,
            lives: 3,
            health: 100,
            color: 'red',
            doubleShot: false,
            shield: false,
            bombs: 3
        };

        // 子弹数组
        let bullets = [];
        // 敌人数组
        let enemies = [];
        // 得分
        let score = 0;
        // 游戏状态
        let gameOver = false;
        let paused = false;
        // 关卡
        let level = 1;
        const levelTarget = 500; // 每关目标得分

        // 炸弹补充计时器
        let bombTimer = 0;

        // 保存游戏数据
        function saveGame() {
            const gameData = {
                score,
                level,
                lives: player.lives,
                health: player.health,
                bombs: player.bombs
            };
            localStorage.setItem('spaceShooterSave', JSON.stringify(gameData));
            alert('游戏已保存！');
        }

        // 加载游戏数据
        function loadGame() {
            const gameData = JSON.parse(localStorage.getItem('spaceShooterSave'));
            if (gameData) {
                score = gameData.score;
                level = gameData.level;
                player.lives = gameData.lives;
                player.health = gameData.health;
                player.bombs = gameData.bombs;
                alert('游戏已加载！');
            } else {
                alert('没有找到保存的游戏数据！');
            }
        }

        // 鼠标移动监听
        canvas.addEventListener('mousemove', (e) => {
            if (!gameOver && !paused) {
                const rect = canvas.getBoundingClientRect();
                player.x = e.clientX - rect.left - player.width/2;
            }
        });

        // 右键炸弹功能
        canvas.addEventListener('contextmenu', (e) => {
            e.preventDefault(); // 阻止默认右键菜单
            if (!gameOver && !paused && player.bombs > 0) {
                player.bombs--;
                enemies = []; // 清除所有敌人
            }
        });

        // 键盘事件监听
        window.addEventListener('keydown', (e) => {
            if (e.key === 'p' || e.key === 'P') {
                paused = !paused; // 暂停/恢复游戏
            }
            if (e.key === 's' || e.key === 'S') {
                saveGame(); // 保存游戏
            }
            if (e.key === 'l' || e.key === 'L') {
                loadGame(); // 加载游戏
            }
        });

        // 射击逻辑
        function shoot() {
            if (!gameOver && !paused) {
                bullets.push({
                    x: player.x + player.width/2 - 2.5,
                    y: player.y,
                    width: 5,
                    height: 10,
                    speed: 10,
                    damage: 5 // 子弹伤害增强
                });
                if (player.doubleShot) {
                    bullets.push({
                        x: player.x + player.width/2 + 10,
                        y: player.y,
                        width: 5,
                        height: 10,
                        speed: 10,
                        damage: 5
                    });
                }
            }
        }

        // 自动射击
        let lastShot = 0;
        setInterval(() => {
            if (!gameOver && !paused) {
                shoot();
            }
        }, 100); // 射击频率提高

        // 生成敌人
        function spawnEnemy() {
            const type = Math.random();
            if (type < 0.7) {
                enemies.push({
                    x: Math.random() * (canvas.width - 30),
                    y: 0,
                    width: 30,
                    height: 30,
                    speed: 3,
                    health: 2 + level, // 敌人血量随关卡增加
                    type: 'normal'
                });
            } else if (type < 0.9) {
                enemies.push({
                    x: Math.random() * (canvas.width - 30),
                    y: 0,
                    width: 30,
                    height: 30,
                    speed: 6,
                    health: 2 + level,
                    type: 'fast'
                });
            } else {
                enemies.push({
                    x: Math.random() * (canvas.width - 30),
                    y: 0,
                    width: 50,
                    height: 50,
                    speed: 2,
                    health: 10 + level * 2,
                    type: 'boss'
                });
            }
        }

        // 游戏循环
        function gameLoop() {
            if (gameOver || paused) return;

            ctx.clearRect(0, 0, canvas.width, canvas.height);

            // 绘制玩家
            ctx.fillStyle = player.color;
            ctx.fillRect(player.x, player.y, player.width, player.height);

            // 更新子弹
            bullets = bullets.filter(bullet => {
                bullet.y -= bullet.speed;
                ctx.fillStyle = 'yellow';
                ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
                return bullet.y > 0;
            });

            // 更新敌人
            if (Math.random() < 0.05) spawnEnemy();
            enemies = enemies.filter(enemy => {
                enemy.y += enemy.speed;
                ctx.fillStyle = enemy.type === 'boss' ? 'purple' : 'blue';
                ctx.fillRect(enemy.x, enemy.y, enemy.width, enemy.height);

                // 碰撞检测
                bullets.forEach((bullet, bi) => {
                    if (checkCollision(bullet, enemy)) {
                        enemy.health -= bullet.damage;
                        if (enemy.health <= 0) {
                            score += enemy.type === 'boss' ? 50 : 10;
                        }
                        bullets.splice(bi, 1);
                    }
                });

                // 敌人到达底部
                if (enemy.y + enemy.height >= canvas.height) {
                    player.health -= 10; // 飞船血量减少10
                    if (player.health <= 0) {
                        player.lives--; // 失去一条命
                        player.health = 100; // 重置血量
                        if (player.lives <= 0) gameOver = true; // 游戏结束
                    }
                    return false; // 移除敌人
                }

                return enemy.health > 0;
            });

            // 炸弹补充
            bombTimer += 1 / 60; // 每帧增加时间
            if (bombTimer >= 30) { // 每30秒补充1颗炸弹
                if (player.bombs < 3) player.bombs++;
                bombTimer = 0;
            }

            // 绘制游戏信息
            ctx.fillStyle = 'white';
            ctx.font = '20px Arial';
            ctx.fillText(`生命: ${player.lives}    血量: ${player.health}    得分: ${score}    关卡: ${level}    炸弹: ${player.bombs}`, 10, 30);

            // 关卡切换
            if (score >= levelTarget * level) {
                level++;
                player.bombs++;
                alert(`进入第 ${level} 关！`);
            }

            // 游戏结束判断
            if (gameOver) {
                ctx.fillStyle = 'red';
                ctx.font = '50px Arial';
                ctx.fillText('GAME OVER', canvas.width/2 - 140, canvas.height/2);
            }

            requestAnimationFrame(gameLoop);
        }

        // 碰撞检测函数
        function checkCollision(rect1, rect2) {
            return rect1.x < rect2.x + rect2.width &&
                   rect1.x + rect1.width > rect2.x &&
                   rect1.y < rect2.y + rect2.height &&
                   rect1.y + rect1.height > rect2.y;
        }

        // 启动游戏
        gameLoop();
    </script>
</body>
</html>